/** 
 * All Rights Reserved , Copyright (C) 2023 , 青岛鼎信通讯股份有限公司
 * 
 *AppMessageSysController
 * APP系统消息
 * 
 * 修改纪录
 * 2023-09-09 版本：1.0 yanshunkuan 创建。
 * @version 版本：1.0
 * @author 作者：yanshunkuan
 * 创建日期：2023-09-09
 */

package com.topscomm.appv2.controller;

import com.topscomm.appv2.controller.auto.AppMessageSysControllerAuto;
import com.topscomm.appv2.util.CommonUtil;
import com.topscomm.basic.util.SQLAdapter;
import com.topscomm.pub.util.BaseUtil;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.pub.vo.Query;
import com.topscomm.pub.vo.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: yanshunkuan
 * @date: 2023-09-09
 * @description:APP系统消息Controller
 * @modify:
 */
@Slf4j
@RestController
@RequestMapping("appv2/appMessageSys")
public class AppMessageSysController extends AppMessageSysControllerAuto implements IAppMessageSysController {

    /**
     * @author: yanshunkuan
     * @date: 2023-09-08 15:42:52
     * @description:拼接查询sql
     * @return:
     * @modify:
     */
    @Override
    protected String spellListSql(Map paraMap) {
        StringBufferProxy sql = new StringBufferProxy();
        sql.appendSingle(" 1=1 ");
        if (paraMap.containsKey("qry_receivercode")) {
            String value = paraMap.get("qry_receivercode").toString();
            if (!StringUtil.isEmpty(value))
                sql.appendSingle(" and receivercode = '{0}' ", value);
        }
        if (paraMap.containsKey("qry_receivername")) {
            String value = paraMap.get("qry_receivername").toString();
            if (!StringUtil.isEmpty(value))
                sql.appendSingle(" and receivername = '{0}' ", value);
        }
        if (paraMap.containsKey("qry_content")) {
            String value = paraMap.get("qry_content").toString();
            if (!StringUtil.isEmpty(value))
                sql.appendSingle(" and content like '%{0}%' ", value);
        }
        if (paraMap.containsKey("qry_subject")) {
            String value = paraMap.get("qry_subject").toString();
            if (!StringUtil.isEmpty(value))
                sql.appendSingle(" and subject like '%{0}%' ", value);
        }
        if (paraMap.containsKey("qry_createon1")) {
            String value = paraMap.get("qry_createon1").toString();
            if (!StringUtil.isEmpty(value))
                sql.appendSingle(" and createon >={0}", SQLAdapter.toDateLong(value + " 00:00:00"));
        }
        if (paraMap.containsKey("qry_createon2")) {
            String value = paraMap.get("qry_createon2").toString();
            if (!StringUtil.isEmpty(value))
                sql.appendSingle(" and createon <={0}", SQLAdapter.toDateLong(value + " 23:59:59"));
        }
        if (paraMap.containsKey("qry_readdate1")) {
            String value = paraMap.get("qry_readdate1").toString();
            if (!StringUtil.isEmpty(value))
                sql.appendSingle(" and readdate  >={0}", SQLAdapter.toDateLong(value + " 00:00:00"));
        }
        if (paraMap.containsKey("qry_readdate2")) {
            String value = paraMap.get("qry_readdate2").toString();
            if (!StringUtil.isEmpty(value))
                sql.appendSingle(" and readdate <={0}", SQLAdapter.toDateLong(value + " 23:59:59"));
        }
        if (paraMap.containsKey("qry_state")) {
            String value = paraMap.get("qry_state").toString();
            if (!StringUtil.isEmpty(value))
                sql.appendSingle(" and state = {0} ", value);
        }
        if (paraMap.containsKey("qry_messagetype")) {
            String value = paraMap.get("qry_messagetype").toString();
            if (!StringUtil.isEmpty(value))
                sql.appendSingle(" and messagetype = '{0}'", value);
        }
        return sql.toString();
    }

	/**
	 * @author: yanshunkuan
	 * @date: 2023-09-04
	 * @param paraMap
	 * @return
	 * @description: 转换出OA迁移的消息接口的查询SQL语句
	 * @modify:
	 */
    protected String spellQueryListSql(Map paraMap) {
        StringBufferProxy sql = new StringBufferProxy();
        sql.appendSingle(" 1=1 ");
        sql.appendSingle(super.spellListSql(paraMap));
        if(!paraMap.containsKey("qry_receivercode")) {
            sql.appendSingle(" and receivercode = '{0}'", this.getUserBean().getUser().getCode());
        }
        if (paraMap.containsKey("qry_receivername")) {
            String value = paraMap.get("qry_receivername").toString();
            if (!StringUtil.isEmpty(value))
                sql.appendSingle(" and receivername = '{0}' ", value);
        }
        if (paraMap.containsKey("qry_sourcetype")) {
            String value = paraMap.get("qry_sourcetype").toString();
            if (!StringUtil.isEmpty(value))
                sql.appendSingle(" and sourcetype like '%{0}%' ", value);
        }
        if (paraMap.containsKey("qry_content")) {
            String value = paraMap.get("qry_content").toString();
            if (!StringUtil.isEmpty(value))
                sql.appendSingle(" and content like '%{0}%' ", value);
        }
        if (paraMap.containsKey("qry_subject")) {
            String value = paraMap.get("qry_subject").toString();
            if (!StringUtil.isEmpty(value))
                sql.appendSingle(" and subject like '%{0}%' ", value);
        }
        if (paraMap.containsKey("qry_contentorsubject")) {
            String value = paraMap.get("qry_contentorsubject").toString();
            if (!StringUtil.isEmpty(value))
                sql.appendSingle(" and (content like '%{0}%' or subject like '%{0}%') ", value);
        }
        if (paraMap.containsKey("qry_state")) {
            String value = paraMap.get("qry_state").toString();
            if (!StringUtil.isEmpty(value))
                sql.appendSingle(" and state = {0} ", value);
        }
        if (paraMap.containsKey("qry_createon1")) {
            String value = paraMap.get("qry_createon1").toString();
            if (!StringUtil.isEmpty(value))
                sql.appendSingle(" and createon >= {0} ", SQLAdapter.toDateLong(value+" 00:00:00"));
        }
        if (paraMap.containsKey("qry_createon2")) {
            String value = paraMap.get("qry_createon2").toString();
            if (!StringUtil.isEmpty(value))
                sql.appendSingle(" and createon <= {0}", SQLAdapter.toDateLong(value+" 23:59:59"));
        }
        if (paraMap.containsKey("qry_readdate1")) {
            String value = paraMap.get("qry_readdate1").toString();
            if (!StringUtil.isEmpty(value))
                sql.appendSingle(" and readdate  >={0}", SQLAdapter.toDateLong(value + " 00:00:00"));
        }
        if (paraMap.containsKey("qry_readdate2")) {
            String value = paraMap.get("qry_readdate2").toString();
            if (!StringUtil.isEmpty(value))
                sql.appendSingle(" and readdate <={0}", SQLAdapter.toDateLong(value + " 23:59:59"));
        }
        if (paraMap.containsKey("qry_messagetype")) {
            String value = paraMap.get("qry_messagetype").toString();
            if (!StringUtil.isEmpty(value))
                sql.appendSingle(" and messagetype = '{0}' ", value);
        }
        return sql.toString();
    }

    /**
     * @author: yanshunkuan
     * @date: 2023-09-08 15:45:20
     * @description: OA迁移的消息查询接口
     * @return:
     * @modify:
     */
    @GetMapping("/querylist")
    public ResponseResult<Object> queryList(@RequestParam(required = false) Map<String, Object> paraMap) {
        try {
            paraMap = BaseUtil.decodeSecureMap(paraMap);
            Query query = new Query(paraMap);
            String sqlWhere = this.spellQueryListSql(paraMap);
            int totalCount = this.service.getCount("receivercode = '" + this.getUserBean().getUser().getCode() + "' and state = 0");
            List<Map<String, Object>> tempList = null;
            if (query.getPageSize() > 0) {
                tempList = this.service.queryMapForPage(sqlWhere, query.getCurrentPage(), query.getPageSize(),
                        query.getSidx(), query.getSord());
            } else {
                tempList = this.service.queryMapByWhere(sqlWhere);
            }
            Map<String, Object> result = new HashMap<>();
            result.put("listDataMap", tempList);
            result.put("records", totalCount);
            return ResponseResult.ok(result);
        } catch (Exception ex) {
            return handleControllerException(ex);
        }
    }

    /**
     * @author: yanshunkuan
     * @date: 2023-09-08 14:03:24
     * @param
     * @description: APP接口，无需token验证，返回系统sys消息数量
     * @return:
     * @modify:
     */
    @GetMapping("/messageNumNoToken")
    public ResponseResult<Object> messageNumNoToken(@RequestParam(required = false) Map<String, Object> paraMap) {
        paraMap = BaseUtil.decodeMap(paraMap);
        long startTime = System.currentTimeMillis();
        // 拼接sql
        StringBufferProxy whereSql = new StringBufferProxy();
        whereSql.appendSingle("receivercode = '{0}'", CommonUtil.getTrimString(paraMap, "usercode"));
        if (paraMap.containsKey("state")) {
            whereSql.appendSingle("and state = {0}", ConvertUtil.convertToString(paraMap.get("state")));
        }
        int count = this.service.getCount(whereSql.toString());
        Map<String, Object> result = new HashMap<>();
        result.put("count", count);
        long consumeTime = System.currentTimeMillis() - startTime;
        log.info("messageNumNoToken接口共耗时" + consumeTime + "ms");
        return ResponseResult.ok(result);
    }
}

